<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="省" prop="provinceId">
      <el-input v-model="dataForm.provinceId" placeholder="省"></el-input>
    </el-form-item>
    <el-form-item label="市" prop="cityId">
      <el-input v-model="dataForm.cityId" placeholder="市"></el-input>
    </el-form-item>
    <el-form-item label="县" prop="countyId">
      <el-input v-model="dataForm.countyId" placeholder="县"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="房子类型:1、住宅，2、写字楼，3、商铺" prop="houseType">
      <el-input v-model="dataForm.houseType" placeholder="房子类型:1、住宅，2、写字楼，3、商铺"></el-input>
    </el-form-item>
    <el-form-item label="租金/月" prop="leaseMoney">
      <el-input v-model="dataForm.leaseMoney" placeholder="租金/月"></el-input>
    </el-form-item>
    <el-form-item label="租赁方式：1、整租，2、合租" prop="leaseWay">
      <el-input v-model="dataForm.leaseWay" placeholder="租赁方式：1、整租，2、合租"></el-input>
    </el-form-item>
    <el-form-item label="室" prop="roomNumber">
      <el-input v-model="dataForm.roomNumber" placeholder="室"></el-input>
    </el-form-item>
    <el-form-item label="厅" prop="livingroomNumber">
      <el-input v-model="dataForm.livingroomNumber" placeholder="厅"></el-input>
    </el-form-item>
    <el-form-item label="卫" prop="restroomNumber">
      <el-input v-model="dataForm.restroomNumber" placeholder="卫"></el-input>
    </el-form-item>
    <el-form-item label="厨" prop="cookroomNumber">
      <el-input v-model="dataForm.cookroomNumber" placeholder="厨"></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="所在层" prop="layerNumber">
      <el-input v-model="dataForm.layerNumber" placeholder="所在层"></el-input>
    </el-form-item>
    <el-form-item label="最大层" prop="maxLayer">
      <el-input v-model="dataForm.maxLayer" placeholder="最大层"></el-input>
    </el-form-item>
    <el-form-item label="付款方式:1、付1 ,  2、押1付1，  3、押2付1   4、押3付1" prop="paymentType">
      <el-input v-model="dataForm.paymentType" placeholder="付款方式:1、付1 ,  2、押1付1，  3、押2付1   4、押3付1"></el-input>
    </el-form-item>
    <el-form-item label="配套设施，json集合 [1,2,3,4,5,6,7]" prop="facility">
      <el-input v-model="dataForm.facility" placeholder="配套设施，json集合 [1,2,3,4,5,6,7]"></el-input>
    </el-form-item>
    <el-form-item label="详细介绍" prop="details">
      <el-input v-model="dataForm.details" placeholder="详细介绍"></el-input>
    </el-form-item>
    <el-form-item label="经纪人编号" prop="brokerId">
      <el-input v-model="dataForm.brokerId" placeholder="经纪人编号"></el-input>
    </el-form-item>
    <el-form-item label="发布时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="发布时间"></el-input>
    </el-form-item>
    <el-form-item label="状态：0、已提交正在审核，1、审核通过已发布，2、审核未通过" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态：0、已提交正在审核，1、审核通过已发布，2、审核未通过"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          provinceId: '',
          cityId: '',
          countyId: '',
          address: '',
          houseType: '',
          leaseMoney: '',
          leaseWay: '',
          roomNumber: '',
          livingroomNumber: '',
          restroomNumber: '',
          cookroomNumber: '',
          area: '',
          layerNumber: '',
          maxLayer: '',
          paymentType: '',
          facility: '',
          details: '',
          brokerId: '',
          createTime: '',
          status: ''
        },
        dataRule: {
          provinceId: [
            { required: true, message: '省不能为空', trigger: 'blur' }
          ],
          cityId: [
            { required: true, message: '市不能为空', trigger: 'blur' }
          ],
          countyId: [
            { required: true, message: '县不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          houseType: [
            { required: true, message: '房子类型:1、住宅，2、写字楼，3、商铺不能为空', trigger: 'blur' }
          ],
          leaseMoney: [
            { required: true, message: '租金/月不能为空', trigger: 'blur' }
          ],
          leaseWay: [
            { required: true, message: '租赁方式：1、整租，2、合租不能为空', trigger: 'blur' }
          ],
          roomNumber: [
            { required: true, message: '室不能为空', trigger: 'blur' }
          ],
          livingroomNumber: [
            { required: true, message: '厅不能为空', trigger: 'blur' }
          ],
          restroomNumber: [
            { required: true, message: '卫不能为空', trigger: 'blur' }
          ],
          cookroomNumber: [
            { required: true, message: '厨不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          layerNumber: [
            { required: true, message: '所在层不能为空', trigger: 'blur' }
          ],
          maxLayer: [
            { required: true, message: '最大层不能为空', trigger: 'blur' }
          ],
          paymentType: [
            { required: true, message: '付款方式:1、付1 ,  2、押1付1，  3、押2付1   4、押3付1不能为空', trigger: 'blur' }
          ],
          facility: [
            { required: true, message: '配套设施，json集合 [1,2,3,4,5,6,7]不能为空', trigger: 'blur' }
          ],
          details: [
            { required: true, message: '详细介绍不能为空', trigger: 'blur' }
          ],
          brokerId: [
            { required: true, message: '经纪人编号不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '发布时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态：0、已提交正在审核，1、审核通过已发布，2、审核未通过不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/house/house/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.provinceId = data.house.provinceId
                this.dataForm.cityId = data.house.cityId
                this.dataForm.countyId = data.house.countyId
                this.dataForm.address = data.house.address
                this.dataForm.houseType = data.house.houseType
                this.dataForm.leaseMoney = data.house.leaseMoney
                this.dataForm.leaseWay = data.house.leaseWay
                this.dataForm.roomNumber = data.house.roomNumber
                this.dataForm.livingroomNumber = data.house.livingroomNumber
                this.dataForm.restroomNumber = data.house.restroomNumber
                this.dataForm.cookroomNumber = data.house.cookroomNumber
                this.dataForm.area = data.house.area
                this.dataForm.layerNumber = data.house.layerNumber
                this.dataForm.maxLayer = data.house.maxLayer
                this.dataForm.paymentType = data.house.paymentType
                this.dataForm.facility = data.house.facility
                this.dataForm.details = data.house.details
                this.dataForm.brokerId = data.house.brokerId
                this.dataForm.createTime = data.house.createTime
                this.dataForm.status = data.house.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/house/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'provinceId': this.dataForm.provinceId,
                'cityId': this.dataForm.cityId,
                'countyId': this.dataForm.countyId,
                'address': this.dataForm.address,
                'houseType': this.dataForm.houseType,
                'leaseMoney': this.dataForm.leaseMoney,
                'leaseWay': this.dataForm.leaseWay,
                'roomNumber': this.dataForm.roomNumber,
                'livingroomNumber': this.dataForm.livingroomNumber,
                'restroomNumber': this.dataForm.restroomNumber,
                'cookroomNumber': this.dataForm.cookroomNumber,
                'area': this.dataForm.area,
                'layerNumber': this.dataForm.layerNumber,
                'maxLayer': this.dataForm.maxLayer,
                'paymentType': this.dataForm.paymentType,
                'facility': this.dataForm.facility,
                'details': this.dataForm.details,
                'brokerId': this.dataForm.brokerId,
                'createTime': this.dataForm.createTime,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
